<?php
namespace Admin\Controller;

use Think\Controller;

class CommonController extends Controller
{

    private $user;
    public function __construct()
    {
        parent::__construct();
        $this->user = M('User');
        $this->check_login();
    }

    public function check_login()
    {
//        dump($_SESSION['user']);
//        dump(isset($_SESSION['user']));
//        exit;
        if(isset($_SESSION['user'])){
//            $this->error('你还没有登录', U("Admin/User/login"));
            $this->assign('user',$_SESSION['user']);
            return;
        }

        //1.检查token是否有值
        if(!isset($_COOKIE['token'])){
            $this->error('你还没有登录', U("Admin/User/login"));
        }else{
            //2.判断token值是否和数据库一致
            $data['token'] = $_COOKIE['token'];
            $rel = $this->user->where($data)->find();
            dump($rel);
            if(!$rel){
                $this->error('你非法登陆', U("Admin/User/login"));
            }

            //3.将值存入session
            $_SESSION['user'] = $rel;
            $this->assign('user',$_SESSION['user']);
        }



    }
}